package com.dehui.traders.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dehui.traders.R;
import com.dehui.traders.widgets.BaseViewHolder;
import com.dehui.traders.widgets.CommonAdapter;
import com.dehui.traders.widgets.MyHScrollView;
import java.util.List;

import me.goldze.mvvmhabit.utils.Utils;

/**
 * Created by hqq on 18.4.25.
 */

public abstract class TableAdapter<T> extends CommonAdapter<T>{

    private final int screenWidth;
    private OnTableData onTableData;
    private final LayoutInflater inflater;

    public TableAdapter(Context context, List mDatas) {
        super(context, mDatas, R.layout.list_item_table_content);
        inflater = LayoutInflater.from(context);
        screenWidth = Utils.getScreen(context).widthPixels;
    }

    public void setOnTableData(OnTableData onTableData){
        this.onTableData = onTableData;
    }

    @Override
    public void convert(BaseViewHolder helper, T item, int position) {

        LinearLayout ll = helper.getView(R.id.ll_scroll);

        MyHScrollView scrollview = helper.getView(R.id.horizontal_scrollview);
        scrollview.setPadding(screenWidth / 2,0,0,0);
        setFragment(scrollview);

        onTableData.addScrollviews(scrollview);

        ll.removeAllViews();
        for (int i = 0; i < getL(); i++){
            View view = inflater.inflate(R.layout.tab_content_item, null);
            TextView tvItem = view.findViewById(R.id.tv_item);
            RelativeLayout.LayoutParams tvlp = (RelativeLayout.LayoutParams) tvItem.getLayoutParams();
            tvlp.width = screenWidth / 4;
            tvItem.setLayoutParams(tvlp);
            setItemText(i,tvItem,item);
            ll.addView(view);
        }

        TextView tv0 = helper.getView(R.id.tv_0);
        TextView tv1 = helper.getView(R.id.tv_1);
        tv0.setText(setTv0(item), TextView.BufferType.SPANNABLE);
        tv1.setText(setTv1(item));
        tv1.setTextColor(Color.RED);
    }

    public abstract void setFragment(MyHScrollView scrollview);

    public abstract void setItemText(int position,TextView tv,T item);

    public abstract SpannableString setTv0(T item);

    public abstract String setTv1(T item);

    public abstract int getL();

    public interface OnTableData{
        void addScrollviews(MyHScrollView scrollview);
    }

}
